import BaseController from '../../core/controllers/Base.controller';
import storageApi from '../../core/storage/storage';

@Inject('$log', '$rootScope', '$state', 'globalService', 'msgService', 'ngDialog', 'ngDialogService')
class MessageController extends BaseController {
  constructor() {
    super();

    this.paginationService = this.msgService;
    this.$setQueryCallback((res) => {
      this.msgListData = res.rows;
    });

    this.mapParams = {};

    this.$paginationQuery();
    // 全选
    this.toggleAllFlag = false;
  }

  /*eslint-disable */
  // 全部设为已读
  $setAllRead() {
    this.ngDialogService.confirm('确定全部设为已读吗？')
      .then((res) => {
        this.msgService
          .isSetAllRead()
          .then((res) => {
            if (res.success) {
              this.ngDialogService.alertSuccess(res.message);
              this.$paginationQuery();
            }
          });
      });
  }

  // 删除全部已读
  $deleteAllRead(id) {
    this.ngDialogService.confirm('确定删除全部已读吗？')
      .then((data) => {
        this.msgService
          .deleteAll()
          .then((res) => {
            if (res.success) {
              this.ngDialogService.alertSuccess(res.message);
              this.$paginationQuery();
            }
          });
      });
  }

  // 批量删除
  $delRead() {
    let ids = [];
    let selectedMsgList = this.$getSelectedMsg();
    let msgStatusCheck = true;
    let dialogPrompt = '';
    if (!selectedMsgList.length) {
      this.ngDialogService.alertError('请选择消息');
      return;
    };
    selectedMsgList.forEach((item) => {
      ids.push(item.id);
    });
    if (msgStatusCheck) {
      this.ngDialogService.confirm('确定删除这些数据吗？')
        .then((data) => {
          this.msgService
            .delete(ids)
            .then((res) => {
              if (res.success) {
                this.ngDialogService.alertSuccess(res.message);
                this.$paginationQuery();
              }
            });
        });
    }
  }

  $toggleAll() {
    this.msgListData.forEach((item) => {
      item.checked = this.toggleAllFlag;
    });
  }

  // 判断全选信息
  $isChange() {
    let bool = true;
    this.msgListData.forEach((item) => {
      if (!item.checked) {
        bool = false;
      }
    });
    this.toggleAllFlag = bool;
  }

  // 获取选中信息
  $getSelectedMsg() {
    let selectedMsgList = [];
    if (this.msgListData) {
      let length = this.msgListData.length;
      for (let i = 0; i < length; i++) {
        let item = this.msgListData[i];
        if (item.checked) {
          selectedMsgList.push(item);
        }
      }
      return selectedMsgList;
    }
  }

  $getMsgType(type) {
    switch (type) {
      case '1':
        return '预警';
        break;
      case '2':
        return '提醒';
        break;
      default:
        break;
    }
  }

  // 跳转到详情
  $goPage(item) {
    if (item.sendSource === '2') {
      this.$state.go('workerInfoDetail', ({id: item.serviceId}));
    } else if (item.sendSource === '3') {
      this.$state.go('deviceInfoDetail', ({id: item.serviceId}));
    } else if (item.sendSource === '4') {
      this.$state.go('patrolDetail', ({id: item.serviceId}));
    }
  }
}

export default MessageController;
